﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmVatTu

    Private lstSource As New List(Of PH_Func_GetAllHoaDon_VatTuResult)

    Private _IDHoaDon As String
    Public Property IDHoaDon() As String
        Get
            Return _IDHoaDon
        End Get
        Set(ByVal value As String)
            _IDHoaDon = value
        End Set
    End Property

    Private _LaNhapTraLai As Boolean
    Public Property LaNhapTraLai() As Boolean
        Get
            Return _LaNhapTraLai
        End Get
        Set(ByVal value As Boolean)
            _LaNhapTraLai = value
        End Set
    End Property

    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private _LaNhap As Boolean
    Public Property LaNhap() As Boolean
        Get
            Return _LaNhap
        End Get
        Set(ByVal value As Boolean)
            _LaNhap = value
        End Set
    End Property

    Private _Thang As Integer
    Public Property Thang() As Integer
        Get
            Return _Thang
        End Get
        Set(ByVal value As Integer)
            _Thang = value
        End Set
    End Property

    Private _Nam As Integer
    Public Property Nam() As Integer
        Get
            Return _Nam
        End Get
        Set(ByVal value As Integer)
            _Nam = value
        End Set
    End Property

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatPhieuNhapKhoVatTu
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        frm.Nam = Nam
        frm.Thang = Thang
        frm.ActionForm = vAddAction
        frm.ShowDialog()
        FillDataToGrid()
    End Sub

    Private Sub FrmVatTu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon Then
            IDHoaDon = grvChiTiet.GetRowCellValue(grvChiTiet.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSua) Then
            btnSua_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If
    End Sub
    Public Shared Them, Sua, Xoa As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSua.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub
    Private Sub FrmVatTu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupControl1.Text = GetLabelPhimTatCoBan("", True, True, True)
        FillDataToGrid()
        PhanQuyen()
    End Sub
    Private Sub FillDataToGrid()
        lstSource = dbTV.PH_Func_GetAllHoaDon_VatTu(Thang, Nam, LaNhap, LaChon, False).ToList
        grdChung.DataSource = (From ds In lstSource Order By ds.NgayCT Descending Select ds).ToList
        grdChung.RefreshDataSource()
    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        If grvChung.FocusedRowHandle < 0 Then
            Return
        End If

        Dim strID As String
        strID = grvChung.GetRowCellValue(grvChung.FocusedRowHandle, grdColID).ToString
        If Not String.IsNullOrEmpty(strID) Then
            If MsgAskDelete("Bạn có muốn xóa hóa đơn nhập này không?") = vMsg.vReSult.Yes Then
                Dim objChung = (From ds In dbTV.VT_HDNhaps Where ds.InUsed AndAlso ds.ID = strID).FirstOrDefault
                If objChung IsNot Nothing Then
                    Using dbTV.Transaction
                        Dim dteNgayXoa As DateTime = dbTV.GetDateTimeServer
                        objChung.InUsed = False
                        objChung.EditedOn = dteNgayXoa
                        For Each item In (From ds In dbTV.VT_HDNhap_ChiTiets Where ds.InUsed AndAlso ds.IDHDNhap = objChung.ID)
                            item.InUsed = False
                            item.EditedOn = dteNgayXoa
                        Next
                        iSubmitChange()
                    End Using
                    FillDataToGrid()
                End If
            End If
        End If
    End Sub

    Private Sub grvChung_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles grvChung.FocusedRowChanged
        Dim index = grvChung.FocusedRowHandle
        Dim id1 = grvChung.GetRowCellValue(index, grdColID)
        If id1 IsNot Nothing Then
            'TAm de cua SGD nhu nay sau chinh funtion sau
            If LaNhap Then
                If LaNhapTraLai Then
                    grdChiTiet.DataSource = (From ds In dbTV.PH_Func_GetAllHoaDon_ChiTiet(id1, LaNhap) Order By ds.MaSP Select ds).ToList
                Else
                    Dim lstHDChiTiet = (From ds In dbTV.VT_HDNhap_ChiTiets Join vt In dbTV.VT_DM_VatTus On ds.IDVatTu Equals vt.ID _
                                        Join kho In dbTV.DM_Khos On ds.IDKho Equals kho.ID _
                                        Where ds.InUsed AndAlso vt.InUsed AndAlso kho.InUsed AndAlso ds.IDHDNhap = CStr(id1) Order By ds.STT _
                                        Select ds.ID, ds.SoHDTC, ds.IDVatTu, ds.IDKho, ds.SoLuong.GetValueOrDefault, ds.DonGia, _
                                        ds.TienThanhToan, ds.ThueSuat, ds.TienThue, ds.TiLeCK, _
                                        ds.ThanhTien, kho.MaKho, vt.MaVatTu, vt.TenVatTu).ToList

                    grdChiTiet.DataSource = lstHDChiTiet

                    'Dim lstKho = (From ds In dbTV.DM_Khos Where ds.InUsed Select ds.ID, ds.MaKho).ToList
                    'grdChiTiet.DataSource = (From ds In lstHDChiTiet Join k In lstKho On ds.IDKho Equals k.ID _
                    '                         Join sp In lstSourceAllSanPham On ds.IDVatTu Equals sp.ID _
                    '                         Select ds.ID, ds.SoHDTC, k.MaKho, sp.MaSanPham, sp.TenSanPham, _
                    '                         ds.SoLuong, ds.DonGia, ds.ThueSuat, TiLeCK = ds.TiLeCK, ds.ThanhTien, ds.TienThanhToan).ToList

                End If
            Else
                Dim lstHDChiTiet = (From ds In dbTV.VT_HDXuat_ChiTiets Join vt In dbTV.VT_DM_VatTus On ds.IDVatTu Equals vt.ID _
                                    Join kho In dbTV.DM_Khos On ds.IDKho Equals kho.ID _
                                    Where ds.InUsed AndAlso vt.InUsed AndAlso kho.InUsed AndAlso ds.IDHDXuat = CStr(id1) Order By ds.STT _
                                    Select ds.ID, ds.IDVatTu, ds.IDKho, ds.SoLuong, ds.DonGia, ds.ThanhTien, ds.ThueSuat, _
                                    ds.TiLeCK, ds.TienThanhToan, kho.MaKho, vt.MaVatTu, vt.TenVatTu).ToList

                ''Dim lstKho = (From ds In dbTV.DM_Khos Where ds.InUsed Select ds.ID, ds.MaKho).ToList
                ''grdChiTiet.DataSource = (From ds In lstHDChiTiet Join k In lstKho On ds.IDKho Equals k.ID _
                ''                         Join sp In lstSourceAllSanPham On ds.IDSanPham Equals sp.ID _
                ''                         Select ds.ID, ds.ThanhTien, DonGia = ds.DonGia, k.MaKho, sp.MaSanPham, _
                ''                          SoLuong = ds.SoLuong, sp.TenSanPham, ds.TienThanhToan, ds.ThueSuat, TiLeCK = ds.TiLeCK).ToList


            End If
        Else
            grdChiTiet.DataSource = Nothing
        End If

        grdChiTiet.RefreshDataSource()

        If index = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle Then
            grvChung.FocusedColumn = grdColTenNCC
        End If
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid()

    End Sub

    Private Sub btnSua_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSua.ItemClick
        If grvChung.FocusedRowHandle < 0 Then
            Return
        End If
        Dim strID = grvChung.GetRowCellValue(grvChung.FocusedRowHandle, grdColID).ToString
        If Not String.IsNullOrEmpty(strID) Then
            If LaNhap Then
                Dim objHDNhap = (From ds In dbTV.VT_HDNhaps Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
                If objHDNhap IsNot Nothing Then
                    Dim frm As New FrmCapNhatPhieuNhapKhoVatTu
                    frm.ActionForm = vEditAction
                    frm.objNhapEdit = objHDNhap
                    frm.Thang = Thang
                    frm.Nam = Nam
                    frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
                    frm.ShowDialog()
                    FillDataToGrid()
                End If
            Else

            End If
        End If
    End Sub

    Private Sub btnIn_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnIn.ItemClick
        If grvChung.FocusedRowHandle < 0 Then
            Return
        End If
        If LaNhap Then
            Dim strID As String = grvChung.GetRowCellValue(grvChung.FocusedRowHandle, grdColID).ToString
            Dim objHDNhap = (From ds In dbTV.VT_HDNhaps Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
            If objHDNhap IsNot Nothing Then
                InHoaDonNhapVT(objHDNhap)
            End If
        Else

        End If
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdChung)
    End Sub
End Class